﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Service.DataMaintenance;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 组合项目维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class ZuHeXiangMuWeiHuController : Controller
    {
        private readonly IZuHeXiangMuWeiHu _IZuHeXiangMuWeiHu;
        private static readonly ILog log = LogManager.GetLogger(typeof(YuanQuWeiHuController));
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICustomJWTService _ICustomJWTService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IZuHeXiangMuWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public ZuHeXiangMuWeiHuController(IZuHeXiangMuWeiHu IZuHeXiangMuWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IZuHeXiangMuWeiHu = IZuHeXiangMuWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }
        /// <summary>
        /// 申请单添加
        /// </summary>
        /// <param name="ShenQingDanAddClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostShenQingDanAdd(ShenQingDanAddClass ShenQingDanAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.PostShenQingDanAdd(ShenQingDanAddClass,YuanQuQianZhui));
        }
        /// <summary>
        /// 申请单修改
        /// </summary>
        /// <param name="ShenQingDanUpdateClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutShenSingDanUpdate(ShenQingDanUpdateClass ShenQingDanUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutShenSingDanUpdate(ShenQingDanUpdateClass));
        }
        /// <summary>
        /// 申请单删除
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteShenQingDanDel( string strShenQingDanID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.DeleteShenQingDanDel(strShenQingDanID));

        }

        /// <summary>
        /// 根据科室ID、查询申请单
        /// </summary>
        /// <param name="strKeShiID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetShenQingDanSel(string strKeShiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetShenQingDanSel(strKeShiID, YuanQu));
        }
        /// <summary>
        /// 组合项目添加
        /// </summary>
        /// <param name="ZuHeXiangMuAddClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostZuHeXiangMuAdd(ZuHeXiangMuAddClass ZuHeXiangMuAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.PostZuHeXiangMuAdd(ZuHeXiangMuAddClass,YuanQuQianZhui));
        }
        /// <summary>
        /// 组合项目修改
        /// </summary>
        /// <param name="ZuHeXiangMuUpdateClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutZuHeXiangMuUpdate(ZuHeXiangMuUpdateClass ZuHeXiangMuUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutZuHeXiangMuUpdate(ZuHeXiangMuUpdateClass));
        }
        /// <summary>
        /// 组合项目删除
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteZuHeXiangMuDel(string strZuHeXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.DeleteZuHeXiangMuDel(strZuHeXiangMuID));

        }
        /// <summary>
        /// 根据申请单ID、所属报告分类查询组合项目
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <param name="SuoShuBaoGaoFenLei"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetZuHeXiangMuSel(string strShenQingDanID, string SuoShuBaoGaoFenLei)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetZuHeXiangMuSel(strShenQingDanID, SuoShuBaoGaoFenLei,YuanQu));
        }

        /// <summary>
        /// 明细项目添加
        /// </summary>
        /// <param name="MingXiXiangMuAddClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostMingXiXiangMuAdd(MingXiXiangMuAddClass MingXiXiangMuAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.PostMingXiXiangMuAdd(MingXiXiangMuAddClass, YuanQuQianZhui));
        }
        /// <summary>
        /// 明细项目修改
        /// </summary>
        /// <param name="MingXiXiangMuUpdateClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutMingXiXiangMuUpdate(MingXiXiangMuUpdateClass MingXiXiangMuUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutMingXiXiangMuUpdate(MingXiXiangMuUpdateClass));
        }
        /// <summary>
        /// 加载全部科室
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetKeShiXinXiSel()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            var UserId = token.Claims.FirstOrDefault(t => t.Type == "UserId")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetKeShiXinXiSel(YuanQu,UserId));
        }
        /// <summary>
        /// 明细项目删除
        /// </summary>
        /// <param name="strMingXiXiangMuID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteMingXiXiangMuDel(string strMingXiXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.DeleteMingXiXiangMuDel(strMingXiXiangMuID));
        }
        /// <summary>
        /// 根据组合项目ID查询明细项目
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetMingXiXiangMuSel(string strZuHeXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.GetMingXiXiangMuSel(strZuHeXiangMuID));
        }
        /// <summary>
        /// 申请单调整顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="ShenQingDanID"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutShenQingDanTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string ShenQingDanID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutShenQingDanTiaoZhengShunXu(iQianShunXu, iHouShunXu, ShenQingDanID));
        }
        /// <summary>
        /// 组合项目调整顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="ZuHeXiangMuID"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutZuHeXiangMuTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string ZuHeXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutZuHeXiangMuTiaoZhengShunXu(iQianShunXu, iHouShunXu, ZuHeXiangMuID));
        }
        /// <summary>
        /// 明细项目调整顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="MingXiXiangMuID"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutMingXiXiangMuTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string MingXiXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutMingXiXiangMuTiaoZhengShunXu(iQianShunXu, iHouShunXu, MingXiXiangMuID));
        }
        /// <summary>
        /// 查询费用类别
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetFeiYongLeiBieSel()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.GetFeiYongLeiBieSel());
        }
        /// <summary>
        /// 查询检验性质
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetJianYanXingZhiSel()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.GetJianYanXingZhiSel());
        }
        /// <summary>
        /// 组合项目检索值查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetJianSuoZuHeXiangMuSel(string strKeShiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetJianSuoZuHeXiangMuSel(strKeShiID, YuanQu));
        }
        /// <summary>
        /// 调整申请单
        /// </summary>
        /// <param name="strXinShenQingDanID"></param>
        /// <param name="strZuHeXiangMuID"></param>
        /// <param name="strXinKeShiID"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutTiaoZhengZuHeXiangMu(string strXinShenQingDanID, string strZuHeXiangMuID, string strXinKeShiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PutTiaoZhengZuHeXiangMu(strXinShenQingDanID, strZuHeXiangMuID, strXinKeShiID));
        }
        /// <summary>
        /// 通用_查询科室申请单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetKeShiShenQingDanSel()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetKeShiShenQingDanSel(YuanQu));
        }
        /// <summary>
        /// 通用_根据申请单ID查询组合项目
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetZuHeXiangMuTongYongSel(string strShenQingDanID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZuHeXiangMuWeiHu.GetZuHeXiangMuTongYongSel(strShenQingDanID,YuanQu));
        }
        /// <summary>
        /// 包含组合项目添加
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <param name="strBaoHanZuHeXiangMuID"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostBaoHanZuHeXiangMuAdd(string strZuHeXiangMuID,string strBaoHanZuHeXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.PostBaoHanZuHeXiangMuAdd(strZuHeXiangMuID,strBaoHanZuHeXiangMuID));
        }
        /// <summary>
        /// 包含组合项目查询
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetBaoHanZuHeXiangMuSel(string strZuHeXiangMuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.GetBaoHanZuHeXiangMuSel(strZuHeXiangMuID));
        }
        /// <summary>
        /// 包含组合项目删除
        /// </summary>
        /// <param name="strBaoHanGuanXiID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteBaoHanZuHeXiangMuDel(string strBaoHanGuanXiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZuHeXiangMuWeiHu.DeleteBaoHanZuHeXiangMuDel(strBaoHanGuanXiID));
        }
    }
}
